//============================================================================
// Name        : rfid-local.cpp
// Author      : KRM
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

unsigned short aSignal1[]={
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640
};
unsigned short aSignal2[]={
		640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,710,
		640,640,640,640,640,730,
		640,640,640,640,640,740,
		800,800,800,800,800,
		640,640,640,640,640,700,
		640,640,640,640,640,700,
		740,800,800,700,
		800,800,800,800,800,

		740,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		640,640,640,640,640,700,
		800,800,690,800,780,
		800,800,800,800,790,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640,
		800,800,800,800,800,
		640,640,640,640,640,640
};
unsigned char aCodeManchester[2][15];

unsigned char decode8_fm2manchester2(unsigned char iCodeLength,unsigned char iNumberArray, unsigned short iPeriodCur,unsigned short iTPeriodBoundary,unsigned short iTPeriodHighMax)
{
  	unsigned char iStateChange=0,iPosition=0,iCurrentByte=0,iCurrentManchBit=0;
  	static unsigned char iCurrentState,iCounterManchBits,iCounterPeriodsDecoding,iCounterHighBits,iCounterLowBits;

  	if (iPeriodCur<iTPeriodBoundary)
  	{
  		if (iCurrentState)
		{
			iStateChange=1;
		}
		iCurrentState=0;
  		iCounterLowBits++;
  	}
  	else
  	{
  		if (iPeriodCur<iTPeriodHighMax)
  		{
  			if (!iCurrentState)
  			{
  				iStateChange=1;
  			}
			iCurrentState=1;
  			iCounterHighBits++;
  		}
  		else
  		{
  			iCounterHighBits=iCounterLowBits=iCounterPeriodsDecoding=iCounterManchBits=0;
  			return 0;
  		}
  	}

  	if (iStateChange)
  	{
  		iStateChange=0;
  		if (iCounterLowBits>iCounterHighBits)
  		{
  			iCounterPeriodsDecoding=iCounterLowBits;
  			iCounterLowBits=0;
  			iCurrentManchBit=0;
  		}
  		else
  		{
  			iCounterPeriodsDecoding=iCounterHighBits;
  			iCounterHighBits=0;
  			iCurrentManchBit=1;
  		}

  		if (iCounterPeriodsDecoding>(3+iCurrentState))
  		{
 			iPosition=7-(iCounterManchBits&7);
 			iCurrentByte=11-(iCounterManchBits>>3);
 			if (iCurrentManchBit) { aCodeManchester[iNumberArray][iCurrentByte]|= (1<<iPosition); }
 			else 				  { aCodeManchester[iNumberArray][iCurrentByte]&= ~(1<<iPosition); }
 			if (iCounterManchBits>iCodeLength)
 			{
 				iCounterHighBits=iCounterLowBits=iCounterPeriodsDecoding=iCounterManchBits=0;
  				return 0x10;
 			}
 			iCounterManchBits++;
  		}

  		if (iCounterPeriodsDecoding>(8+(iCurrentState<<1)))
  		{
 			iPosition=7-(iCounterManchBits&7);
 			iCurrentByte=11-(iCounterManchBits>>3);
 			if (iCurrentManchBit) { aCodeManchester[iNumberArray][iCurrentByte]|= (1<<iPosition); }
 			else 				  { aCodeManchester[iNumberArray][iCurrentByte]&= ~(1<<iPosition); }
 			if (iCounterManchBits>iCodeLength)
 			{
 				iCounterHighBits=iCounterLowBits=iCounterPeriodsDecoding=iCounterManchBits=0;
 				return 0x10;
 			}
 			iCounterManchBits++;
  		}

  		if (iCounterPeriodsDecoding>(13+((iCurrentState<<1)|iCurrentState)))
  		{
 			iPosition=7-(iCounterManchBits&7);
 			iCurrentByte=11-(iCounterManchBits>>3);
 			if (iCurrentManchBit) { aCodeManchester[iNumberArray][iCurrentByte]|=	(1<<iPosition); }
 			else 				  { aCodeManchester[iNumberArray][iCurrentByte]&=  ~(1<<iPosition); }
 			if (iCounterManchBits>iCodeLength)
 			{
 				iCounterHighBits=iCounterLowBits=iCounterPeriodsDecoding=iCounterManchBits=0;
 				return 0x10;
 			}
 			iCounterManchBits++;
  		}
  		iCounterPeriodsDecoding=0;
  	}
  	return 1;
}

int main()
{
	unsigned char bitReq;
	unsigned short iSizeArray;
	iSizeArray=sizeof(aSignal2);
	for (int i=1;i<600;i++)
	{
		bitReq=decode8_fm2manchester2(94,0,aSignal2[i],720,1000);
		if (bitReq==0x10)
		{
			cout << "Elements =" << iSizeArray<<endl; // prints !!!Hello World!!!
			return 0;
		}
	}
	cout << "No result" << endl; // prints !!!Hello World!!!
	return 0;
}
